package com.example.ymm.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.sql.Timestamp;
import java.util.Date;

@Slf4j
public class FileUtil {

    public static String  domian = "H:/read_book_server/";
    public static String  host = "http://220.161.77.66:10111/";

    /**
     * 上传mp3
     * 
     * @param oggUrl
     * @param createTime
     * @return
     */
    public static String uploadOggFn(String oggUrl, Timestamp createTime) {
        String result = null;

        String path = "app_file/mp3/";
        String savePath = domian + path;
        try {
            String fileName = oggUrl.split("https://static.dingtalk.com/media/")[1];
            fileName = fileName.replaceAll("\\.ogg", "\\.mp3");
            String month = DateKit.dateFormat(new Date(createTime.getTime()), "yyyy-MM");

            String dates =month+"/"+ DateKit.dateFormat(new Date(createTime.getTime()), "yyyy-MM-dd");
            savePath += dates + "/" + fileName;
            result = host + path + dates + "/" + fileName;
            File file = new File(savePath);
            if (!file.getParentFile().exists()) {
                FileUtils.forceMkdirParent(file); // 使用commons-io的工具类
            }
            URL url = new URL(oggUrl);
            ConvertingAnyAudioToMp3.amrInputStreamToMp3(url, file);
            // result = uploadByInputStream(file, createTime);
            // file.delete();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * 普通图片上传到本地目录
     *
     * @param file
     * @param
     * @return
     */
    public static String uploadImgToLocal(MultipartFile file) {

        String fileName = file.getOriginalFilename();
        String month = DateKit.dateFormat(new Date(), "yyyy-MM");

        String dates =month+"/"+ DateKit.dateFormat(new Date(), "yyyy-MM-dd");

        String result = null;
        String path = "app_file/images/";
        String savePath = domian + path;
        // path="D:/file/";
        try {
            savePath += dates + "/" + fileName;
            result = host + path + dates + "/" + fileName;

            File toFile = new File(savePath);
            if (!toFile.getParentFile().exists()) {
                FileUtils.forceMkdirParent(toFile); // 使用commons-io的工具类
            }
            // System.out.println("savePath::" + savePath);
            // System.out.println("--是否存在:" + toFile.exists());
            InputStream ins = file.getInputStream();

            inputStreamToFile(ins, toFile);
            ins.close();
        } catch (Exception e) {
            log.error("--保存文件失败:"+e.getMessage(), e);

        }

        return result;
    }

    // 获取流文件
    public static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String deleteLocalFile(String url){
        String result="";
        if(url.contains(host)){
            String replace = url.replace(host, "");
            String savePath = domian + replace;
            File toFile = new File(savePath);
            if(toFile.exists()){
                toFile.delete();
                result="删除成功!";
            }
        }
        return result;
    }


}
